using System;
using System.Collections;
using UnityEngine;

[AddComponentMenu("NGUI/Interaction/JoyStick")]
public class UIJoyStick : MonoBehaviour
{
	public enum DragEffect
	{
		None,
		Momentum,
		MomentumAndSpring
	}

	public Action JoyStickUp;

	public float joyStickPosX;

	public float joyStickPosY;

	private float posDivision;

	public Transform target;

	private Vector3 scale = Vector3.one;

	public float scrollWheelFactor;

	public bool restrictWithinPanel;

	public float ClampRadius = 100f;

	private Plane mPlane;

	private Vector3 mLastPos;

	private UIPanel mPanel;

	private bool mPressed;

	private Vector3 mMomentum = Vector3.zero;

	private float mScroll;

	private Bounds mBounds;

	private Vector3 targetPosition = Vector3.zero;

	public float strength = 10f;

	public bool ignoreTimeScale;

	private float mThreshold;

	public Vector2 Value => new Vector2(joyStickPosX, joyStickPosY);

	private void Awake()
	{
		posDivision = 1f / ClampRadius;
	}

	private void FindPanel()
	{
		mPanel = ((!(target != null)) ? null : UIPanel.Find(target.transform, createIfMissing: false));
		if (mPanel == null)
		{
			restrictWithinPanel = false;
		}
	}

	private void OnHover(bool Hover)
	{
	}

	private void OnPress(bool pressed)
	{
		if (!base.enabled || !NGUITools.GetActive(base.gameObject) || !(target != null))
		{
			return;
		}
		mPressed = pressed;
		if (pressed)
		{
			if (restrictWithinPanel && mPanel == null)
			{
				FindPanel();
			}
			if (restrictWithinPanel)
			{
				mBounds = NGUIMath.CalculateRelativeWidgetBounds(mPanel.cachedTransform, target);
			}
			mMomentum = Vector3.zero;
			mScroll = 0f;
			SpringPosition component = target.GetComponent<SpringPosition>();
			if (component != null)
			{
				component.enabled = false;
			}
			mLastPos = UICamera.lastHit.point;
			Transform transform = UICamera.currentCamera.transform;
			mPlane = new Plane(((!(mPanel != null)) ? transform.rotation : mPanel.cachedTransform.rotation) * Vector3.back, mLastPos);
		}
		else if (restrictWithinPanel && mPanel.clipping != 0)
		{
			mPanel.ConstrainTargetToBounds(target, ref mBounds, immediate: false);
		}
		if (!pressed)
		{
			StartCoroutine("SpringPositionUpdate");
			if (JoyStickUp != null)
			{
				JoyStickUp();
			}
		}
	}

	private void OnDrag(Vector2 delta)
	{
		if (base.enabled && NGUITools.GetActive(base.gameObject) && target != null)
		{
			UICamera.currentTouch.clickNotification = UICamera.ClickNotification.BasedOnDelta;
			Ray ray = UICamera.currentCamera.ScreenPointToRay(UICamera.currentTouch.pos);
			float enter = 0f;
			if (mPlane.Raycast(ray, out enter))
			{
				Vector3 position = mLastPos = ray.GetPoint(enter);
				target.position = position;
				Transform transform = target.transform;
				Vector3 vector = Vector3.ClampMagnitude(target.transform.localPosition, ClampRadius);
				float x = vector.x;
				Vector3 vector2 = Vector3.ClampMagnitude(target.transform.localPosition, ClampRadius);
				transform.localPosition = new Vector3(x, vector2.y, 0f);
				Vector3 localPosition = target.transform.localPosition;
				joyStickPosX = localPosition.x * posDivision;
				Vector3 localPosition2 = target.transform.localPosition;
				joyStickPosY = localPosition2.y * posDivision;
			}
		}
	}

	private void LateUpdate()
	{
		float deltaTime = RealTime.deltaTime;
		if (target == null)
		{
			return;
		}
		if (mPressed)
		{
			SpringPosition component = target.GetComponent<SpringPosition>();
			if (component != null)
			{
				component.enabled = false;
			}
			mScroll = 0f;
		}
		else
		{
			mMomentum += scale * ((0f - mScroll) * 0.05f);
			mScroll = NGUIMath.SpringLerp(mScroll, 0f, 20f, deltaTime);
			if (mMomentum.magnitude > 0.0001f)
			{
				if (mPanel == null)
				{
					FindPanel();
				}
				if (mPanel != null)
				{
					target.position += NGUIMath.SpringDampen(ref mMomentum, 9f, deltaTime);
					if (restrictWithinPanel && mPanel.clipping != 0)
					{
						mBounds = NGUIMath.CalculateRelativeWidgetBounds(mPanel.cachedTransform, target);
					}
					return;
				}
			}
			else
			{
				mScroll = 0f;
			}
		}
		NGUIMath.SpringDampen(ref mMomentum, 9f, deltaTime);
	}

	private void OnScroll(float delta)
	{
		if (base.enabled && NGUITools.GetActive(base.gameObject))
		{
			if (Mathf.Sign(mScroll) != Mathf.Sign(delta))
			{
				mScroll = 0f;
			}
			mScroll += delta * scrollWheelFactor;
		}
	}

	private IEnumerator SpringPositionUpdate()
	{
		while (true)
		{
			float delta = (!ignoreTimeScale) ? Time.deltaTime : RealTime.deltaTime;
			if (mThreshold == 0f)
			{
				mThreshold = (targetPosition - target.localPosition).magnitude * 0.001f;
			}
			target.localPosition = NGUIMath.SpringLerp(target.localPosition, targetPosition, strength, delta);
			Vector3 localPosition = target.transform.localPosition;
			joyStickPosX = localPosition.x * posDivision;
			Vector3 localPosition2 = target.transform.localPosition;
			joyStickPosY = localPosition2.y * posDivision;
			if (mThreshold >= (targetPosition - target.localPosition).magnitude)
			{
				break;
			}
			yield return 0;
		}
		target.localPosition = targetPosition;
		joyStickPosX = 0f;
		joyStickPosY = 0f;
	}
}
